package ui;

import java.io.File;

import javax.swing.KeyStroke;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.IntellitypeListener;
import com.melloware.jintellitype.JIntellitype;
import com.melloware.jintellitype.JIntellitypeConstants;
import com.tulskiy.keymaster.common.HotKey;
import com.tulskiy.keymaster.common.HotKeyListener;
import com.tulskiy.keymaster.common.Provider;

public class GlobalKeyboardGrabber
{

	private static boolean isWindows;
	private static Provider keyBoardInstance;

	public static void setup() {
		keyBoardInstance = null;
		if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
			isWindows = true;
		} else {
			isWindows = false;
		}

		if (isWindows) {
			String libLocation = new File(".").getAbsolutePath();
			libLocation = libLocation.substring(0, libLocation.length() - 1);
			libLocation += "lib\\com\\melloware\\jintellitype\\JIntellitype.dll";
			
			String is64Bit = System.getenv("PROCESSOR_ARCHITECTURE");
			if (!"".equals(is64Bit)) {
				libLocation = libLocation.replace("JIntellitype.dll", "JIntellitype64.dll");
			}
			
			JIntellitype.setLibraryLocation(libLocation);
			
			if (JIntellitype.isJIntellitypeSupported() && (!JIntellitype.checkInstanceAlreadyRunning("QuickToDo"))) {
				JIntellitype.getInstance();				
				JIntellitype.getInstance().registerHotKey(1, "CTRL+SHIFT+D");//JIntellitypeConstants.MOD_CONTROL, (int)'J');
			}
		} else {
			keyBoardInstance = Provider.getCurrentProvider(true);		
		}
	}

	public static void shutdown() {
		if (isWindows) {
			JIntellitype.getInstance().cleanUp();
		} else {
			keyBoardInstance.reset();
			keyBoardInstance.stop();
		}
	}

	public static void addHotKeyListener(final ui.GlobalHotKeyListener listener) {
		if (isWindows) {
			JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {

				@Override
				public void onHotKey(int hotKey) {
					listener.onHotKey();
				}
				
			});
		} else {
			keyBoardInstance.register(KeyStroke.getKeyStroke("control shift D"), new HotKeyListener() {

				@Override
				public void onHotKey(HotKey hotKey) {
					listener.onHotKey();
				}

			});
		}
	}
}
